/*动态加载的全局变量*/
var count,loaded,start;
/*url的全局变量*/
var urlDisCount = "musicDislike/countDislikeMusic";
var urlDisList = "musicDislike/getDislikeMusicList";
var urlDisDel = "musicDislike/deleteDislikeMusic";
var urlPreCount = "musicPreference/countPreferenceMusic";
var urlPreList = "musicPreference/getPreferenceMusicList";
var urlPreDel = "musicPreference/deletePreferenceMusic";
var urlAccCount = "musicAccumulation/countAccumulationMusic";
var urlAccList = "musicAccumulation/getAccumulationMusic";
var urlAccDel = "musicAccumulation/deleteAccumulationMusic";
/*判断现在处于哪个列表显示*/
var type;
$(function(){
	/*
	 * 音乐列表播放与删除
	 * */
	$('.fi-music').mouseenter(function(){
		$('.fi-music').css('color','rgba(128,128,128,0.5)');
	});
	$('.fi-music').mouseleave(function(){
		$('.fi-music').css('color','rgba(128,128,128,1)');
	});
	$('.fi-x').mouseenter(function(){
		$('.fi-x').css('color','rgba(128,128,128,0.5)');
});
	$('.fi-x').mouseleave(function(){
		$('.fi-x').css('color','rgba(128,128,128,1)');
	});

    $('.fi-x').live("click",function(){
        var musicId = $(this).attr("value");
        var url = (type=='acc'?urlAccDel:((type=='pre')?urlPreDel:urlDisDel));
        $.ajax({
            type : "post",
            url : url,
            data : {
                "userId" :  $('#userId').val(),
                "musicId" : musicId
            },
            dataType : "json",
            success : function(data){
                if (data.result == true) {
                    var id = (type=='acc'?'#musicAcc':((type=='pre')?'#musicPre':'#musicDis'));
                      $(id).trigger('click');
                }
            }
        });
    });

    /*滑动条监听事件的注册*/
    $(window).scroll(function(){
        // 当滚动后高度超过200时,有返回至顶部的标签
        var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //获取滚动后的高度
        if( scrollt >200 ){
            //判断滚动后高度超过200px,就显示
            $("#backTop").fadeIn(400); //淡出
        }else{
            //如果返回或者没有超过,就淡入,否则会出现闪动
            $("#backTop").fadeOut(200);
        }

        // 当滚动到最底部以上100像素时， 加载新内容
        if (loaded&&($(document).height() - $(this).scrollTop() - $(this).height()<100)){
            var url = (type=='acc'?urlAccList:((type=='pre')?urlPreList:urlDisList));
            loaded = $.loadMore(url);
        }
    });

    $("#backTop").click(function(){
        //当点击标签的时候,使用animate在200毫秒的时间内,滚到顶部
        $("html,body").animate({scrollTop:"0px"},200);
    });

    /*获取列表按钮的click事件的注册*/
    $('#musicPre').click(function () {
        $("#record_viewer").css('display','inline');
        $('#pw_modify').css('display','none');
        $('#user-info').css('display','none');
        type = 'pre';
        start = 0;
        $('#info-music >div').remove();
        count = $.countMusic(urlPreCount);
        loaded = $.loadMore(urlPreList);
    });

    $('#musicDis').click(function(){
        $("#record_viewer").css('display','inline');
        $('#pw_modify').css('display','none');
        $('#user-info').css('display','none');
        type = 'dis';
        start = 0;
        $('#info-music >div').remove();
        count = $.countMusic(urlDisCount);
        loaded = $.loadMore(urlDisList);
    });

    $('#musicAcc').click(function(){
        $("#record_viewer").css('display','inline');
        $('#pw_modify').css('display','none');
        $('#user-info').css('display','none');
        type = 'acc';
        start = 0;
        $('#info-music >div').remove();
        count = $.countMusic(urlAccCount);
        loaded = $.loadMore(urlAccList);
    });

  /*
   * 点击个人资料
   * */
    $('#myInfo').click(function(){
		$("#record_viewer").css('display','none');
        $('#pw_modify').css('display','none');
		$('#user-info').css('display','inline');

	});
	$('#modify').click(function(){
        $("#record_viewer").css('display','none');
		$('#user-info').css('display','none');
	    $('#pw_modify').css('display','inline');
	});
	
	$('#submit').click(function(){
		if($("#newPw").val()==$('#validPw').val()&&($("#newPw").val()!=null)&&($('#validPw').val()!=null)){
			
			$.ajax({
				type : "post",
	            url : "user/updateUser",
	            data : {
	               'userId' : $('#userId').val(),
	               'userPw' : $("#newPw").val()
	            },
	            dataType : "json",
	            success : function(data){
	                if (data.result == true) {
	                	$('#myInfo').trigger("click");
	                }
	                else alert("修改密码错误");
	            }
			});
		}
	});

});

/*
jQuery.fillData = function(){
    $.ajax({
        type : "post",
        url : "musicPreference/getPreferenceMusicList",
        data : {
            "userId" :  $('#userId').val()
        },
        dataType : "json",
        success : function(data){
            $('#record_viewer div:not(.header)').remove();
            if (data != null) {
                $.each(data,function(index,item){
                    $('#record_viewer').append(
                            "<div class='info-wrapper'><div class='song_info'>" +
                            "<div class='prop><p class='song_title'>" +
                            item.musicName+"</p><br><p class='performer'>"+
                            item.singer+"</p></div></div><div class='action'><div class='fi-music' value='"+item.musicId+"'></div>" +
                            "<div class='fi-x' value='"+item.musicId+"'></div></div></div></div>"
                    );
                });
            }
        }
    });
};
*/

/*动态加载(类似分页形式)获取音乐列表的接口*/
jQuery.loadMore = function(url){
    var  result = true;
    $.ajax({
        type : "post",
        url : url,
        data : {
            "userId" :  $('#userId').val(),
            "limit" : 5,
            "start" : start
        },
        async: false,
        dataType : "json",
        success : function(data)
        {
            start+=5;
            count-=5;
            if (data != null) {

//                $('#record_viewer div:not(.header)').remove();
                var oProduct, $row, iHeight, iTempHeight;
                for(var i=0, l=data.length; i<l; i++)
                {
                    oProduct = data[i];

                    // 找出当前高度最小的列, 新内容添加到该列
                    iHeight = -1;
                    $('#record_viewer >div:not(.header)').each(function(){
                        iTempHeight = Number( $(this).height() );
                        if(iHeight==-1 || iHeight>iTempHeight)
                        {
                            iHeight = iTempHeight;
                            $row = $(this);
                        }
                    });
                    $item = $("<div class='info-wrapper'><div class='song_info'><img src='"+oProduct.cover+"' >" +
                        "<div class='prop><p class='song_title'>" +
                        oProduct.musicName+"</p><br><p class='performer'>"+
                        oProduct.singer+"</p></div></div><div class='action'><div class='fi-music' value='"+oProduct.musicId+"'></div>" +
                        "<div class='fi-x' value='"+oProduct.musicId+"'></div></div></div></div>").hide();

                    $row.append($item);
                    $item.fadeIn();
                }
                if(count<0)
                    result = false
            }
            else {
                result = false;
            }
        }
    });
    return result;
};

/*获取音乐列表的数量的接口*/
jQuery.countMusic = function(url){
    var count;
    $.ajax({
        type : "post",
        url : url,
        data : {
            "userId" :  $('#userId').val()
        },
        async: false,
        dataType : "json",
        success : function(data)
        {
            count = data;
        }
    });
    return count;
};